package com.example.apiproject.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.apiproject.entity.JoinActivity;
import com.example.apiproject.jwt.jwtutil;
import com.example.apiproject.service.JoinActivityService;
import com.example.apiproject.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (JoinActivity)表控制层
 *
 * @author makejava
 * @since 2023-09-23 13:40:57
 */
@RestController
@RequestMapping("/joinActivity")
public class JoinActivityController {
    /**
     * 服务对象
     */
    @Resource
    private JoinActivityService joinActivityService;
    @Autowired
    private jwtutil jwtUtil;


    /**
     * 分页查询
     *
     * @param joinActivity 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<JoinActivity>> queryByPage(JoinActivity joinActivity, PageRequest pageRequest) {
        return ResponseEntity.ok(this.joinActivityService.queryByPage(joinActivity, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<JoinActivity> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.joinActivityService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param joinActivity 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<JoinActivity> add(JoinActivity joinActivity) {
        return ResponseEntity.ok(this.joinActivityService.insert(joinActivity));
    }

    /**
     * 报名活动
     * @param request
     * @param id 活动id
     * @return
     */
    @PostMapping("/signUp")

    public R signUp(HttpServletRequest request, String id,String description){
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            token = request.getParameter("token");
        }
        int  userId = jwtUtil.getUserId(token);
        if(joinActivityService.querySigned(userId,id)!=0)  {return R.error("已报名成功，不要重复报名");}
        if(joinActivityService.signUp(userId,id,description)==1)  return R.ok();
      return   R.error("人数已经满了，去其他活动看看吧！");

    }
    /**
     * 判断是否参加活动
     * @param request
     * @param id 活动id
     * @return 状态值
     */
    @GetMapping("/judge")
    public R judges(HttpServletRequest request, String id){
        String token = request.getHeader("token");
        if(StringUtils.isBlank(token)){
            token = request.getParameter("token");
        }
        int  userId = jwtUtil.getUserId(token);
        if(joinActivityService.querySigned(userId,id)!=0)
            return R.ok("1");
        else
            return R.ok("0");
    }
    /**
     * 编辑数据
     *
     * @param joinActivity 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<JoinActivity> edit(JoinActivity joinActivity) {
        return ResponseEntity.ok(this.joinActivityService.update(joinActivity));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.joinActivityService.deleteById(id));
    }

}

